<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="品牌名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入品牌名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="品牌状态" prop="isEnable">
        <el-select v-model="queryParams.isEnable" placeholder="请选择品牌状态" clearable size="small" >
          <el-option
            v-for="dict in dict.type.sys_is_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall-manager:goods-brand:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall-manager:goods-brand:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleChgEnable(null,1)"
          v-hasPermi="['mall-manager:goods-brand:chgEnable']"
        >启用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="multiple"
          @click="handleChgEnable(null,0)"
          v-hasPermi="['mall-manager:goods-brand:chgEnable']"
        >禁用</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tableDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="品牌名称" align="center" width="250" prop="name" />
      <el-table-column label="品牌图标" align="center" prop="logo" >
        <template slot-scope="scope">
          <el-image style="width: 100px" fit="contain" :src="scope.row.logo"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isEnable" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.isEnable"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item v-hasPermi="['mall-manager:goods-brand:chgEnable']" v-if="scope.row.isEnable == 0">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-check"
                  @click="handleChgEnable(scope.row,1)"
                >启用
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['mall-manager:goods-brand:chgEnable']" v-if="scope.row.isEnable == 1">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-close"
                  @click="handleChgEnable(scope.row,0)"
                >禁用
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item  v-hasPermi="['mall-manager:goods-brand:edit']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['mall-manager:goods-brand:remove']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  维护品牌  -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" width="50%" @close="open = false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logo">
          <UploadPicInput :file-type="fileType" :value="form.logo" :file-size="fileSize" @input="uploadSuccess"></UploadPicInput>
        </el-form-item>
        <el-form-item label="品牌备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBrands,chgBrands,getBrand, addBrand,editBrand, delBrand } from "@/api/mall/goods/brand";
import UploadPicInput from '@/components/UploadPicInput'
export default {
  name: "goods",
  dicts: ['sys_is_enable'],
  components: {UploadPicInput},
  data() {
    return {
      fileType: ['png', 'jpg', 'jpeg'],
      fileSize: 2,
      title: '',
      id: "", //要操作的id
      loading: true, // 表单加载状态
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        isEnable: null,
      },
      form: {
        name: "",
        logo: undefined,
        remark: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "品牌名称不能为空", trigger: "blur" },
          { min: 1, max: 20, message: '品牌名称必须小于20个字符', trigger: 'blur' }
        ],
      },
      submitLoading: false, // 添加或编辑提交状态
      total: 0, // 表单数据总数
      tableDataList: [],
      open: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listBrands(this.queryParams).then(response => {
        this.tableDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    uploadSuccess(res){
      this.form.logo = res;
    },
    /** 上架操作  */
    handleUpper(row){
      const goodsIds = row.id ? [row.id] : this.ids
      let param = {
        goodsIds: goodsIds
      }
      this.$modal.confirm('是否确认上架商品编号为"' + goodsIds + '"的数据项？').then(function() {
        return upGoods(param);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("上架成功");
      }).catch(() => {});
    },
    /** 新增操作  */
    handleAdd(){
      this.open = true;
      this.title = "新增品牌";
      this.reset();
    },
    reset(){
      this.form = {
        name: "",
        logo: undefined,
        remark: ''
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBrand(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改品牌";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delBrand(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 启停按钮操作 */
    handleChgEnable(row,isEnable) {
      const ids = (row && row.id) ? [row.id] :  this.ids;
      let enableDesp = 1 == isEnable ? '启用' : '禁用'
      this.$modal.confirm('是否确认'+enableDesp+'编号为"' + ids + '"的数据项？').then(function() {
        let param = {
          ids: ids,
          isEnable: isEnable
        }
        return chgBrands(param);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(enableDesp + "操作成功");
      }).catch(() => {});
    },
    // 提交
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            editBrand(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBrand(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
